<div class="container">

    <div class="page-title ui-widget-content ui-corner-all head">
	    <div class="headLeft">
        	<h1 class="icon-category">Category Manager</h1>
        </div>
    	<div class="headRight">
       		<div id="toolbar" class="toolbar-list">
            	<ul>
                    <li id="toolbar-new" class="button">
                    	<a class="toolbar"  href="<?php echo base_url('cp/category/add') ?>"><span class="icon-32-new"></span>&nbsp;&nbsp;New</a>
                    </li>
                     <li id="toolbar-trash" class="button">
						<a class="toolbar ui-icon-trash" href="javascript:void(0)"><span class="icon-32-trash"></span>&nbsp;&nbsp;Trash</a>
					</li>

                </ul>
            </div>
        </div>
    </div>
    <div class="portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all form-container">
    <div class="portlet-header ui-widget-header" id="search-post" style="cursor: pointer;"><span class="ui-icon ui-icon-circle-arrow-s"></span>
        Tìm kiếm crawler</div>
    <div class="portlet-content" style="display: block;">
        <form id="formSearch" class="forms" method="get" action="<?php echo base_url('cp/category'); ?>">

        <table class="searchForm">
            <tbody><tr>
                <td><label class="desc">Từ khóa</label></td>
                <td><input type="text" value="<?php echo $param['keyword']; ?>" style="width:155px;" class="field text" name="keyword"></td>
                <td><label class="desc">Level 1</label></td>
                <td><select name="parent" id="parent" class="select">
<option selected="selected" value="0">===Tất cả===</option>
                    <?php
                    foreach($this->all_cats as $key=>$val){
                        if($val['parent']==0)
                            if($val['id']==$param['parent']){
                                echo '<option value="'.$val['id'].'" selected>'.$val['name'].'</option>';
                            } else {
                                echo '<option value="'.$val['id'].'">'.$val['name'].'</option>';
                            }
                    }
                    ?>
</select></td>
                <td><label class="desc">Child</label></td>

                <td><select name="child" id="child" class="select">
<option value="0">===Tất cả===</option>
                    <?php
                    if(!empty($param['parent']))
                        foreach($this->all_cats as $val){
                            if ($val['parent']==$param['parent'])
                                if($val['id']==$param['child']){
                                    echo '<option value="'.$val['id'].'" selected="selected">'.$val['name'].'</option>';
                                }else {
                                    echo '<option value="'.$val['id'].'">'.$val['name'].'</option>';
                                }
                        }
                    ?>
</select>  </td>

                <td colspan="8"><input type="submit" name="dosearch" class="submit" value="Tìm kiếm"></td>
            </tr>


        </tbody></table>


        </form>
    </div>
</div>

    <div class="hastable">
        <?php
        if ($this->session->flashdata('result') != ''){
            echo $this->session->flashdata('result');
        }
        ?>
        <?php echo form_open('cp/category/deleteList',"class='pager-form' id='formlist' name='myform'");?>

        <table>
            <thead>
                <tr>
                    <th style="border-left:0px;"><input type="checkbox" class="submit" onclick="this.value=check(this.form['id[]'])" value="check_none"></th>
                    <th align="left">Name</th>
                    <th>Parent</th>
                    <th>Level</th>
                    <th>orderNo</th>
                    <th style="width: 8%;">Action</th>
                </tr>
            </thead>
            <tbody>
            <?php if(count($records)){
				foreach($records as $key=>$item){
			?>
	            <tr>
                    <td class="center" style="border-left:0px;">
                    	<input type="checkbox" value="<?php echo $item['id']; ?>" name="id[]" class="checkbox">
                    </td>
                    <td><?php echo $item['name']; ?></td>
                    <td><?php echo $item['parent']; ?></td>
                    <td><?php echo $item['level']; ?></td>
                    <td><?php echo $item['orderNo']; ?></td>
                    <td>
                        <div style="margin: 0 auto;">
                       <a class="btn_no_text btn ui-state-default ui-corner-all edit" href="<?php echo base_url('cp/category/edit/'.$item['id']); ?>" >
                        <span class="ui-icon ui-icon-pencil"></span></a>
                        <a class="btn_no_text btn ui-state-default ui-corner-all delete" href="javascript:void(0)" datafld="<?php echo $item['id'] ?>"><span class="ui-icon ui-icon-trash"></span></a>
                        </div></td>

                </tr>
             <?php } } ?>
                </tbody>
        </table>

            <?php echo form_close();?>
    </div>
    <div class="pagination">
        <?php echo $pagination ?>   </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $(".hastable .ui-icon-trash").parent().click(function(){
            if(!confirm("Bạn chắc chắn muốn xóa không?")) {
                return false;
            }
            var id = $(this).attr('datafld');
            jQuery.ajax({
                type: "GET",
                async: false,
                dataType:'json',
                url: base_url + "/category/delete/"+id,
                data: "",
                success: function(data){

                    window.location.reload();
                }
            });
        });

        $(".button .ui-icon-trash").parent().click(function(){
            if($("input:checked").length>0){
                if(!confirm("Bạn chắc chắn muốn xóa không?")) {
                    return false;
                }
                $("#formlist").submit();
            }else{
                alert("Bạn chưa chọn bản nghi để xóa!")

            }
        });

        jQuery('#parent').change(function(){
            var id = $('#parent option:selected').val();
            jQuery.ajax({
                type: "GET",
                async: false,
                dataType:'json',
                url: base_url + "/category/get_subcats/"+id,
                data: "",
                success: function(data){
                    console.log();
                    jQuery('#child').html(data['option']);

                }
            });
        });

    });
</script>